<template>
  <div class="algorithm-comparison">
    <el-row :gutter="20">
      <el-col :span="12">
        <div class="chart-card">
          <div class="chart-title">算法性能对比</div>
          <div class="chart" ref="performanceChartRef"></div>
        </div>
      </el-col>
      <el-col :span="12">
        <div class="chart-card">
          <div class="chart-title">算法效率评估</div>
          <div class="chart" ref="efficiencyChartRef"></div>
        </div>
      </el-col>
    </el-row>
    
    <el-row :gutter="20" class="mt-20">
      <el-col :span="24">
        <div class="metrics-card">
          <el-table :data="metricsData" border style="width: 100%">
            <el-table-column prop="algorithm" label="算法" width="180" />
            <el-table-column prop="accuracy" label="准确率" width="120">
              <template #default="scope">
                {{ scope.row.accuracy }}%
              </template>
            </el-table-column>
            <el-table-column prop="speed" label="处理速度" width="120">
              <template #default="scope">
                {{ scope.row.speed }}s
              </template>
            </el-table-column>
            <el-table-column prop="memory" label="内存占用" width="120">
              <template #default="scope">
                {{ scope.row.memory }}MB
              </template>
            </el-table-column>
            <el-table-column prop="score" label="综合评分" width="120">
              <template #default="scope">
                <el-rate
                  v-model="scope.row.score"
                  disabled
                  show-score
                  text-color="#ff9900"
                />
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, nextTick, onUnmounted, onActivated, watch } from 'vue'
import { useRoute } from 'vue-router'
import * as echarts from 'echarts'

const route = useRoute()
const performanceChartRef = ref<HTMLElement>()
const efficiencyChartRef = ref<HTMLElement>()
let performanceChart: echarts.ECharts | null = null
let efficiencyChart: echarts.ECharts | null = null

// 模拟数据
const metricsData = [
  {
    algorithm: '深度学习分割算法A',
    accuracy: 95.8,
    speed: 2.5,
    memory: 1024,
    score: 4.5
  },
  {
    algorithm: '传统分割算法B',
    accuracy: 88.5,
    speed: 1.8,
    memory: 512,
    score: 3.5
  },
  {
    algorithm: '混合分割算法C',
    accuracy: 92.3,
    speed: 3.2,
    memory: 768,
    score: 4.0
  }
]

const initCharts = async () => {
  // 确保DOM已经更新
  await nextTick()
  
  // 设置一个小延时确保容器已完全渲染
  setTimeout(() => {
    if (performanceChartRef.value && efficiencyChartRef.value) {
      initPerformanceChart()
      initEfficiencyChart()
    }
  }, 100)
}

const initPerformanceChart = () => {
  if (!performanceChartRef.value) return
  
  if (performanceChart) {
    performanceChart.dispose()
  }
  
  performanceChart = echarts.init(performanceChartRef.value)
  const option = {
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      }
    },
    legend: {
      data: ['准确率', '召回率', 'F1分数'],
      top: 10
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      top: 60,
      containLabel: true
    },
    xAxis: {
      type: 'value',
      boundaryGap: [0, 0.01]
    },
    yAxis: {
      type: 'category',
      data: ['算法A', '算法B', '算法C']
    },
    series: [
      {
        name: '准确率',
        type: 'bar',
        data: [95.8, 88.5, 92.3],
        itemStyle: {
          color: '#409EFF'
        }
      },
      {
        name: '召回率',
        type: 'bar',
        data: [94.2, 87.8, 91.5],
        itemStyle: {
          color: '#67C23A'
        }
      },
      {
        name: 'F1分数',
        type: 'bar',
        data: [95.0, 88.1, 91.9],
        itemStyle: {
          color: '#E6A23C'
        }
      }
    ]
  }
  
  performanceChart.setOption(option)
}

const initEfficiencyChart = () => {
  if (!efficiencyChartRef.value) return
  
  if (efficiencyChart) {
    efficiencyChart.dispose()
  }
  
  efficiencyChart = echarts.init(efficiencyChartRef.value)
  const option = {
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'cross'
      }
    },
    legend: {
      data: ['处理速度', '内存占用'],
      top: 10
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      top: 60,
      containLabel: true
    },
    xAxis: {
      type: 'category',
      data: ['算法A', '算法B', '算法C']
    },
    yAxis: [
      {
        type: 'value',
        name: '处理速度(s)',
        position: 'left'
      },
      {
        type: 'value',
        name: '内存占用(MB)',
        position: 'right'
      }
    ],
    series: [
      {
        name: '处理速度',
        type: 'line',
        data: [2.5, 1.8, 3.2],
        smooth: true,
        itemStyle: {
          color: '#409EFF'
        }
      },
      {
        name: '内存占用',
        type: 'bar',
        yAxisIndex: 1,
        data: [1024, 512, 768],
        itemStyle: {
          color: '#67C23A'
        }
      }
    ]
  }
  
  efficiencyChart.setOption(option)
}

const handleResize = () => {
  if (performanceChart && efficiencyChart) {
    performanceChart.resize()
    efficiencyChart.resize()
  }
}

// 监听路由变化
watch(
  () => route.path,
  () => {
    nextTick(() => {
      initCharts()
    })
  }
)

// 监听组件可见性
const observer = new IntersectionObserver(
  (entries) => {
    entries.forEach(entry => {
      if (entry.isIntersecting) {
        initCharts()
      }
    })
  },
  { threshold: 0.1 }
)

onMounted(async () => {
  await initCharts()
  window.addEventListener('resize', handleResize)
  
  // 观察组件可见性
  if (performanceChartRef.value) {
    observer.observe(performanceChartRef.value)
  }
})

onActivated(() => {
  initCharts()
})

onUnmounted(() => {
  window.removeEventListener('resize', handleResize)
  if (performanceChart) {
    performanceChart.dispose()
    performanceChart = null
  }
  if (efficiencyChart) {
    efficiencyChart.dispose()
    efficiencyChart = null
  }
  observer.disconnect()
})
</script>

<style scoped>
.algorithm-comparison {
  padding: 20px;
  height: 100%;
  box-sizing: border-box;
}

.chart-card {
  background: #fff;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  height: 400px;
  position: relative;
}

.chart-title {
  font-size: 16px;
  font-weight: bold;
  margin-bottom: 20px;
  text-align: center;
}

.chart {
  height: calc(100% - 56px);
  width: 100%;
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 0 20px 20px;
  box-sizing: border-box;
}

.mt-20 {
  margin-top: 20px;
}

.metrics-card {
  background: #fff;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}
</style> 